﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace reminder_csh {
    public partial class Form4 : Form {

        private WorkerManagerImpl Wmanager;
        private TaskManagerImpl Tmanager;
        public Worker worker=new Worker();



        public Form4() {
            InitializeComponent();
            Wmanager=new WorkerManagerImpl();
            Tmanager=new TaskManagerImpl();
            taskDataGridView.DataSource=Tmanager.FindAllTasks();
        }

        private void button1_Click(object sender, EventArgs e) {

            if (taskDataGridView.SelectedRows.Count==0)
                return;
            else {
                foreach (DataGridViewRow r in taskDataGridView.SelectedRows) {
                    Task task=new Task();
                    task.Id=int.Parse(r.Cells[0].Value.ToString());
                    task.Name=r.Cells[1].Value.ToString();
                    task.Description=r.Cells[2].Value.ToString();
                    task.Done=bool.Parse(r.Cells[3].Value.ToString());
                    task.Deadline=DateTime.Parse(r.Cells[4].Value.ToString());
                    task.Workers=new List<Worker>();
                    worker.Tasks.Add(task);
                }
            }


            //taskCurrentDataGridView.Refresh();

            //var row = taskDataGridView.CurrentRow;
            //taskCurrentDataGridView.Rows.Add(row);

            var man=new AssignmentManagerImpl();
            using (man) {

                man.AssignTaskToWorker(taskDataGridView.CurrentRow.DataBoundItem as Task, worker);
                taskCurrentDataGridView.DataSource=man.FindTasksOfWorker(worker);
            }
            

        }

        private void Form4_Load(object sender, EventArgs e) {
            nameTextBox.Text=worker.Name;
            emailTextBox.Text=worker.Email;


            pictureBox1.Image=new Bitmap(worker.PicturePath);
            this.Controls.Add(pictureBox1);


            var list=new List<Task>();
            list.Add(new Task {
                Deadline=DateTime.Now,
                Description="bla",
                Done=false,
                Id=524,
                Name="bla",
                Workers=new List<Worker>()

            });

            worker.Tasks.Add(new Task {
                Deadline=DateTime.Now,
                Description="workerTask",
                Done=false,
                Id=524,
                Name="workerTask",
                Workers=new List<Worker>()

            });

            taskCurrentDataGridView.DataSource=worker.Tasks;
            listBox1.Items.AddRange(worker.Tasks.ToArray());

            
        }

        private void button2_Click(object sender, EventArgs e) {
            Wmanager.UpdatePerson(worker);
            
        }

        protected override void OnClosed(EventArgs e)
        {
            Wmanager.Dispose();

            Tmanager.Dispose();
            base.OnClosed(e);
        }
    }
}